package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.user.Address;
import com.qingcheng.service.order.CartService;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.user.AddressService;
import com.qingcheng.service.user.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Welcome to the most Amazing IntelliJ IIIDEA.
 * Creat0r: ^_^ fffantianxing ^_^
 * Date: 2019-07-18
 * Time: 11:13
 */
@RestController
@RequestMapping("cart")
public class CartController {

    @Reference
    private CartService cartService;
    @Reference
    private AddressService addressService;
    @Reference
    private OrderService orderService;

    @GetMapping("findList")
    public List<Map<String, Object>> findCartList() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return cartService.findCartList(username);
    }

    @GetMapping("addCart")
    public Result addToCart(String skuId, Integer num){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addCart(skuId,username,num);
        return new Result();
    }

    @GetMapping("buy")
    public void buy(HttpServletResponse response,String skuId, Integer num) throws IOException {
        addToCart(skuId,num);
        response.sendRedirect("/cart.html");
    }

    @GetMapping("updateChecked")
    public void updateChecked(boolean checked, String skuId){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.updateChecked(checked,skuId,username);
    }

    @GetMapping("deleteChecked")
    public void deleteChecked(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.deleteCheckedCart(username);
    }

    @GetMapping("preferential")
    public int preferential(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        int preferential = cartService.preferential(username);
        return preferential;
    }

    @GetMapping("findNewOrderList")
    public List<Map<String, Object>> findNewOrderList(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return cartService.findNewOrderList(username);
    }

    @GetMapping("findAddress")
    public List<Address> findAddress(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Map map = new HashMap();
        map.put("username",username);
        return addressService.findList(map);
    }

    @PostMapping("save")
    public Map<String,String> save(@RequestBody Order order){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        order.setUsername(username);
        Map<String, String> map = orderService.saveOrder(order);
        return map;
    }
}
